package com.flaremars.picker.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.flaremars.picker.common.DrawingObject;
import com.flaremars.picker.utils.DrawingManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by FlareMars on 2016/3/2.
 */
public class DrawingView extends View {

    private int eraseSize = 20;

    private Context context;
    private Paint paint;
    private Paint regionSelector;
    private DrawingObject temp;
    private int width;
    private int height;
    private Bitmap bitmap;
    private Canvas can;
    private Bitmap bitmap_temp;

    private List<DrawingObject> saveDrawingObjects = new ArrayList<>();
    private List<DrawingObject> deleteDrawingObjects = new ArrayList<>();

    public DrawingView(Context context) {
        super(context);
        this.context=context;
    }

    public DrawingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        init(context,getMeasuredWidth(),getMeasuredHeight());
    }

    public void init (Context context,int width,int height) {
        this.context=context;

        paint = new Paint();
        paint.setAlpha(255);
        paint.setColor(Color.WHITE);

        //透明橡皮擦需要
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//        paint.setStrokeJoin(Paint.Join.ROUND);
//        paint.setStrokeCap(Paint.Cap.ROUND);

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(eraseSize);

        regionSelector = new Paint();
        regionSelector.setStyle(Paint.Style.FILL);
        regionSelector.setAntiAlias(true);
        regionSelector.setColor(DrawingManager.HALF_BLACK);

        this.width=width;
        this.height=height;
        setBackgroundColor(Color.TRANSPARENT);
    }

    public void restore() {
        if (deleteDrawingObjects.size() < 1) {
            return;
        }
        temp = deleteDrawingObjects.remove(deleteDrawingObjects.size() - 1);
        saveDrawingObjects.add(temp);
        invalidate();
    }

    public void cancel() {
        if (saveDrawingObjects.size() < 1) {
            return;
        }
        deleteDrawingObjects.add(saveDrawingObjects.remove(saveDrawingObjects.size() - 1));

        reloadCanvas();
        for (DrawingObject temp : saveDrawingObjects) {
            temp.draw(can,paint);
        }

        invalidate();
    }

    public void setBitmap(Bitmap src) {
        saveDrawingObjects.clear();
        deleteDrawingObjects.clear();
        bitmap=Bitmap.createScaledBitmap(src,width,height,true);
        DrawingManager.INSTANCE.setOriginBtmap(bitmap);
        reloadCanvas();
        invalidate();
    }

    private void reloadCanvas() {
        setBackgroundColor(Color.TRANSPARENT);
        bitmap_temp=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        can=new Canvas();
        can.setBitmap(bitmap_temp);
        can.drawBitmap(bitmap, 0, 0, null);
        resetDrawingObject();
    }

    public void setEraseSize(int size) {
        eraseSize = size;
        paint.setStrokeWidth(eraseSize);
    }

    public Bitmap getBitmap() {
        return bitmap_temp.copy(Bitmap.Config.ARGB_8888,false);
    }

    public void setViewSize(int width,int height) {
        this.width = width;
        this.height = height;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap_temp, 0, 0, null);
        temp.draw(can,paint);
    }

    public void touch_down(MotionEvent event){
        resetDrawingObject();
        temp.handleTouchDown(event);
    }
    public void touch_move(MotionEvent event){
        temp.handleTouchMove(event);
    }
    public void touch_up(MotionEvent event){
        temp.handleTouchUp(event);
        saveDrawingObjects.add(temp);
    }

    private void resetDrawingObject() {
        temp = new PathDrawing(eraseSize);
//        temp = new RectangleDrawing();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
                touch_down(event);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(event);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up(event);
                invalidate();
                break;
        }
        return true;
    }

}